package com.tempjob.admin.config;

import com.anwen.mongo.handlers.MetaObjectHandler;
import com.tempjob.common.context.AdminContext;
import com.tempjob.common.context.AdminContextManage;
import org.bson.Document;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author hui
 * @Date 2024/11/27 23:09
 **/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

//    @Override
//    public void insertFill(AutoFillMetaObject insertAutoFillMetaObject) {
//        AdminContext adminUserContext = AdminContextManage.getContext();
//        if (adminUserContext != null) {
//            insertAutoFillMetaObject.fillValue("oper_id", adminUserContext.getId());
//            insertAutoFillMetaObject.fillValue("oper_name", adminUserContext.getRealName());
//            insertAutoFillMetaObject.fillValue("create_id", adminUserContext.getId());
//            insertAutoFillMetaObject.fillValue("create_name", adminUserContext.getRealName());
//        }
//        insertAutoFillMetaObject.fillValue("oper_time", LocalDateTime.now());
//        insertAutoFillMetaObject.fillValue("create_time", LocalDateTime.now());
//    }
//
//    @Override
//    public void updateFill(AutoFillMetaObject updateAutoFillMetaObject) {
//        AdminContext adminUserContext = AdminContextManage.getContext();
//        if (adminUserContext != null) {
//            updateAutoFillMetaObject.fillValue("oper_id", adminUserContext.getId());
//            updateAutoFillMetaObject.fillValue("oper_name", adminUserContext.getRealName());
//        }
//        updateAutoFillMetaObject.fillValue("oper_time", LocalDateTime.now());
//    }

    @Override
    public void insertFill(Map<String, Object> insertFillMap, Document document) {
        AdminContext adminUserContext = AdminContextManage.getContext();
        if (adminUserContext != null) {
            insertFillMap.put("oper_id", adminUserContext.getId());
            insertFillMap.put("oper_name", adminUserContext.getRealName());
            insertFillMap.put("create_id", adminUserContext.getId());
            insertFillMap.put("create_name", adminUserContext.getRealName());
        }
        insertFillMap.put("oper_time", LocalDateTime.now());
        insertFillMap.put("create_time", LocalDateTime.now());
    }

    @Override
    public void updateFill(Map<String, Object> updateFillMap, Document document) {
        AdminContext adminUserContext = AdminContextManage.getContext();
        if (adminUserContext != null) {
            updateFillMap.put("oper_id", adminUserContext.getId());
            updateFillMap.put("oper_name", adminUserContext.getRealName());
        }
        updateFillMap.put("oper_time", LocalDateTime.now());
    }


}
